package org.yoyo.game;

import java.io.IOException;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public class GamerSprite extends Sprite{

	private static final String imageFile= "/player/player.png";
	private static final int fWidht = 16;
	private static final int fHeight = 32;
	
	public static final int ORDINAL_UP = 0;
	public static final int ORDINAL_RIGHT = 1;
	public static final int ORDINAL_DOWN = 2;
	public static final int ORDINAL_LEFT = 3;
	
	private static final int[][] FRAME_INDEX = new int[][]{		
		{3,4,5},
		{6,7,8},
		{0,1,2},
		{9,10,11}
	};
	
	private static GamerSprite instance = null;
	
	private static  int lastOrdinal = ORDINAL_DOWN;
	
	private GamerSprite(Image image, int frameWidth, int frameHeight) {
		super(image, frameWidth, frameHeight);
	}

	public static GamerSprite getInstance() throws IOException{		
		if(null == instance){
			Image image = Image.createImage(imageFile);
			instance = new GamerSprite(image,fWidht,fHeight);
			//instance.setFrame(0);
		}
		return instance;
	}
	
	public void setOrdinal(int ordinal){
		System.out.println("currentOrdinal:" + ordinal);
		if(lastOrdinal != ordinal)
		{
			lastOrdinal = ordinal;
			setFrameSequence(FRAME_INDEX[ordinal]);
		}
		nextFrame();
	}
}
